//
//  RadioButtonView.m
//  HealthSanMenDoc
//
//  Created by 毛健辉 on 16/7/20.
//  Copyright © 2016年 wonders. All rights reserved.
//

#import "RadioButtonView.h"
#import "FollowCollectionViewCell.h"
#import "AppDelegate.h"
@implementation RadioButtonView

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    _selectNum=0;
    _itemWidthArr=[[NSMutableArray alloc]initWithArray:[self returnWidthOfItem:_itemArr]];

    UICollectionViewLeftAlignedLayout *flowLayout=[[UICollectionViewLeftAlignedLayout alloc] init];
    _collectionView=[[UICollectionView alloc]initWithFrame:frame collectionViewLayout:flowLayout];
    [self addSubview:_collectionView];
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    _collectionView.scrollEnabled=NO;
    [_collectionView registerClass:[FollowCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _itemArr.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return _itemSpace;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FollowCollectionViewCell *cell=(FollowCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.titleLabel.text=_itemArr[indexPath.row];
    NSString *string=_itemWidthArr[indexPath.row];
    CGFloat titleLabelWidth=[string floatValue];
    [cell.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(titleLabelWidth-21);
    }];
    [cell.circleView setHidden:YES];
    if (collectionView==_collectionView) {
        if (indexPath.row==_selectNum) {
            [cell.circleView setHidden:NO];
        }
    }
    return cell;
    
    
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width=[_itemWidthArr[indexPath.row] floatValue];
    CGSize size= CGSizeMake(width, 20);
    return size;
    
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return YES;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    for (int i=0; i<_itemArr.count; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
        FollowCollectionViewCell * cell = ( FollowCollectionViewCell *)[collectionView cellForItemAtIndexPath:path];
        [cell.circleView setHidden:YES];
    }
    FollowCollectionViewCell * cell = ( FollowCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell.circleView setHidden:NO];
    if (collectionView==_collectionView) {
        _selectNum=indexPath.row;
    }
    
    
    
    
}

-(NSArray *)returnWidthOfItem:(NSArray *)array{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    for (NSString *string in array) {
        CGSize labelsize = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 16) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
        CGFloat width=21+labelsize.width+changeSize;
        [arr addObject:[NSString stringWithFormat:@"%f",width]];
    }
    
    
    return arr;
}
-(void)setSelectRow:(NSInteger)row{
    _selectNum=row;
    [self.collectionView reloadData];

}
@end
